// @ts-nocheck
import data_storage from '@ohos.data.storage';

export default {
    data: {
        title: 'World'
    },
    getStorage(){
        const PATH = '/data/accounts/account_0/appdata/com.example.myapplication/database/{storage_name}';
        let store = data_storage.getStorageSync(PATH);
        data_storage.getStorage(PATH, function(err, data) {
            data.getSync();
        });
        const promise = data_storage.getStorage(PATH);
        promise.then((data) => {
            data.clearSync();
        }).catch((err) => {
        });
    },
    deleteStorage(){
        const PATH = '/data/accounts/account_0/appdata/com.example.myapplication/database/{storage_name}';
        // callback方式
        data_storage.deleteStorage(PATH, function(err, data) {
        });
        // promise 方式
//        const promise = data_storage.deleteStorage(PATH);
//        promise.then((data) => {
//        }).catch((err) => {
//        });
    },
    removeStorage(){
        const PATH = '/data/accounts/account_0/appdata/com.example.myapplication/database/{storage_name}';
        data_storage.removeStorageFromCacheSync(PATH);
        // 方式二 promise
//        const promise = data_storage.removeStorageFromCache(PATH);
//        promise.then((data) => {
//        }).catch((err) => {
//        });
    },
    get(){
        console.log('get');
        let ret = store.getSync("key", "test");
        store.get("key", 'test', function(err, data) {
      console.info(data);
});
        const promise = store.get("key", 'test');
        promise.then((data) => {
            console.info(data);
        }).catch((err) => {
        });
    },
    put(){
        console.log('put');
    let data = store.putSync("key", "newValue");
        console.log(data)
    store.put("key1", 'callback', function(err, data) {
        console.log(err)
        console.log(data)
    });
    const promise = store.put("key1", 'promise');
    promise.then((data) => {
        console.log(data)
    }).catch((err) => {
    });
    },
    has(){
        console.log('has')
        let ret = store.hasSync("key");
        store.has("key1", 'test', function(err, data) {
            console.info(data);
        });
        const promise = store.has("key2");
        promise.then((data) => {
            console.info(data);
        }).catch((err) => {
        });
    },
    delete(){
        store.deleteSync("key");
        store.delete("key1", function(err, data) {
            console.log(err)
            console.log(data)
        });
        const promise = store.delete("key2");
        promise.then((data) => {
            console.log(err)
        }).catch((err) => {
        });
    },
    flush(){
        console.log('flush');
        store.flushSync();
        store.flush(function(err, data) {
        });
        const promise = store.flush("key");
        promise.then((data) => {
        }).catch((err) => {
        });
    },
    clear(){
        console.log('clear');
        store.clearSync();
        store.clear(function(err, data) {
        });
        const promise = store.clear();
        promise.then((data) => {
        }).catch((err) => {
        });
    },
    on(){
        console.log('on');
        var observer = function (data) {
            console.info(data);
        };
        store.on('change', observer);
    },
    off(){
        console.log('off');
        var observer = function (data) {
            console.info(data);
        };
        store.off('change', observer);
    },

}
